package com.xtoad.ecms.common.utils.generate;

import java.text.MessageFormat;
import java.util.Map;

/**
 * 控制器生成类
 *
 * @author xtoad
 * @date 2021/1/19
 */
public class ControllerGenerator implements IGenerator {

    /**
     * 根据包路径和类名生成类
     *
     * @param packagePath 包路径
     * @param modelName   类名
     * @return 生成的类
     */
    @Override
    public String generate(String packagePath, String modelName) {
        System.out.println("generate Controller start ");
        Map<String, String> classComment = Generator.getClassComment(Generator.modelPackage + "." + modelName);
        StringBuilder sb = new StringBuilder();
        sb.append("/*\r\n")
                .append(" * Copyright  2019-2029  xtoad\r\n")
                .append(" */\r\n")
                .append("package ").append(packagePath).append(";\r\n")
                .append("\r\n")
                .append("import com.xtoad.ecms.baseinfo.dto.{0}DTO;\r\n")
                .append("import com.xtoad.ecms.baseinfo.service.I{0}Service;\r\n")
                .append("import com.xtoad.ecms.common.web.base.Result;\r\n")
                .append("import org.springframework.web.bind.annotation.PathVariable;\r\n")
                .append("import org.springframework.web.bind.annotation.RequestBody;\r\n")
                .append("import org.springframework.web.bind.annotation.RequestMapping;\r\n")
                .append("import org.springframework.web.bind.annotation.RequestMethod;\r\n")
                .append("import org.springframework.web.bind.annotation.RestController;\r\n")
                .append("\r\n")
                .append("import javax.annotation.Resource;\r\n")
                .append("import javax.validation.Valid;\r\n")
                .append("import java.util.List;\r\n")
                .append("\r\n")
                .append("/**\r\n")
                .append(" * {2}控制器\r\n")
                .append(" *\r\n")
                .append(" * @author ").append(Generator.author).append("\r\n")
                .append(" * @date ").append(Generator.dateString).append("\r\n")
                .append(" */\r\n")
                .append("@RestController\r\n")
                .append("@RequestMapping(\"{1}\")\r\n")
                .append("public class {0}Controller '{'\r\n")
                .append("\r\n")
                .append("    @Resource\r\n")
                .append("    private I{0}Service {1}Service;\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 根据id查询{2}信息\r\n")
                .append("     *\r\n")
                .append("     * @param id {2}id\r\n")
                .append("     * @return 查询结果\r\n")
                .append("     */\r\n")
                .append("    @RequestMapping(value = \"/'{'id'}'\", method = RequestMethod.GET)\r\n")
                .append("    public Result<{0}DTO> get{0}ById(@PathVariable final Long id) '{'\r\n")
                .append("        {0}DTO {1} = {1}Service.get{0}ById(id);\r\n")
                .append("\r\n")
                .append("        return Result.success({1});\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 查询全部{2}信息，不分页\r\n")
                .append("     *\r\n")
                .append("     * @return 全部{2}信息\r\n")
                .append("     */\r\n")
                .append("    @RequestMapping(value = \"/\", method = RequestMethod.GET)\r\n")
                .append("    public Result<List<{0}DTO>> getAll{0}() '{'\r\n")
                .append("        List<{0}DTO> {1}s = {1}Service.getAll{0}();\r\n")
                .append("\r\n")
                .append("        return Result.success({1}s);\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 新增{2}\r\n")
                .append("     *\r\n")
                .append("     * @param {1}DTO {2}信息\r\n")
                .append("     * @return 更新结果\r\n")
                .append("     */\r\n")
                .append("    @RequestMapping(value = \"/add\", method = RequestMethod.POST)\r\n")
                .append("    public Result<{0}DTO> insert{0}(@Valid @RequestBody final {0}DTO {1}DTO) '{'\r\n")
                .append("        {0}DTO ins{0} = {1}Service.insert({1}DTO);\r\n")
                .append("\r\n")
                .append("        return Result.success(ins{0});\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 更新{2}信息\r\n")
                .append("     *\r\n")
                .append("     * @param {1}DTO 更新内容\r\n")
                .append("     * @return 更新结果\r\n")
                .append("     */\r\n")
                .append("    @RequestMapping(value = \"/'{'id'}'\", method = RequestMethod.PUT)\r\n")
                .append("    public Result<{0}DTO> update{0}(@PathVariable final Long id, @Valid @RequestBody final {0}DTO {1}DTO) '{'\r\n")
                .append("        {1}DTO.setId(id);\r\n")
                .append("        {0}DTO upd{0} = {1}Service.update({1}DTO);\r\n")
                .append("\r\n")
                .append("        return Result.success(upd{0});\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 删除{2}信息\r\n")
                .append("     *\r\n")
                .append("     * @param id 删除对象id\r\n")
                .append("     * @return\r\n")
                .append("     */\r\n")
                .append("    @RequestMapping(value = \"/'{'id'}'\", method = RequestMethod.DELETE)\r\n")
                .append("    public Result<{0}DTO> delete{0}(@PathVariable final Long id) '{'\r\n")
                .append("        {1}Service.deleteById(id);\r\n")
                .append("\r\n")
                .append("        return Result.success();\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 批量新增\r\n")
                .append("     *\r\n")
                .append("     * @param {1}s {2}信息\r\n")
                .append("     * @return 更新结果\r\n")
                .append("     */\r\n")
                .append("    @RequestMapping(value = \"/batch/add\", method = RequestMethod.POST)\r\n")
                .append("    public Result<List<{0}DTO>> batchAdd{0}(@Valid @RequestBody final List<{0}DTO> {1}s) '{'\r\n")
                .append("        List<{0}DTO> ins{0}s = {1}Service.batchInsert({1}s);\r\n")
                .append("\r\n")
                .append("        return Result.success(ins{0}s);\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 批量更新\r\n")
                .append("     *\r\n")
                .append("     * @param {1}s {2}信息\r\n")
                .append("     * @return 更新结果\r\n")
                .append("     */\r\n")
                .append("    @RequestMapping(value = \"/batch/modify\", method = RequestMethod.POST)\r\n")
                .append("    public Result<List<{0}DTO>> batchUpd{0}(@Valid @RequestBody final List<{0}DTO> {1}s) '{'\r\n")
                .append("        List<{0}DTO> upd{0}s = {1}Service.batchUpdate({1}s);\r\n")
                .append("\r\n")
                .append("        return Result.success(upd{0}s);\r\n")
                .append("    '}'\r\n")
                .append("\r\n")
                .append("    /**\r\n")
                .append("     * 批量删除\r\n")
                .append("     *\r\n")
                .append("     * @param {1}s {2}信息\r\n")
                .append("     * @return 更新结果\r\n")
                .append("     */\r\n")
                .append("    @RequestMapping(value = \"/batch/delete\", method = RequestMethod.POST)\r\n")
                .append("    public Result<List<{0}DTO>> batchDel{0}(@Valid @RequestBody final List<{0}DTO> {1}s) '{'\r\n")
                .append("        {1}Service.batchDelete({1}s);\r\n")
                .append("\r\n")
                .append("        return Result.success();\r\n")
                .append("    '}'\r\n")
                .append("'}'\r\n");
        System.out.println("generate Controller end ");
        return MessageFormat.format(sb.toString(), modelName, Generator.firstCharLower(modelName), classComment.get(modelName));
    }
}
